package com.yy.yyui
{
	import com.yy.tools.arry.ArrayTools;
	import com.yy.tools.graphics.GT;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 *按钮组 常用于选项卡
	 * @author YY
	 * 
	 */
	public class ButtonGroup implements IActionList
	{
		private var _items:Array;
		private var _actions:Object;
		private var _lock:Boolean;
		
		public function ButtonGroup()
		{
			_items = new Array();
			this._lock = false;
			
			this._actions = {};
			this.actions.onSwitch = null;
		}
		
		public function get actions():Object{
			return this._actions;
		}
		
		private function get items():Array{
			return this._items;
		}
		
		public function get numItems():int{
			return this.items.length;
		}
		
		public function get itemsArray():Array{
			return ArrayTools.getValueArray(this.items,"target");
		}
		
		public function removeAllItems():void{
			for(var i:int = 0,l:int = this.items.length;i<l;i++){
				var obj:Object = this.items[i];
				this.items[i] = null;		
				if(obj.oldOnSelect!=null){
					obj.target.onSelect = obj.oldOnSelect;
				}
				obj.target.removeEventListener(Event.REMOVED_FROM_STAGE, onDestory);
			}
			this.items.length=0;			
		}
		
		/**
		 *查找按钮组中是否有 该元素 
		 * @param item
		 * @return 
		 * 
		 */
		private function findAddeditem(item:Object):Object{
			var i:int;
			var len:int = this.items.length;
			for(i=0;i<len;i++){
				if(items[i].target == item){
					return items[i];
				}
			}
			return null;
		}
		
		public function set onSwitch(value:Function):void{
			this.actions.onSwitch = value;
		}
		
		public function get onSwitch():Function{
			return this.actions.onSwitch;
		}
		
		/**
		 *点击按钮组元素处理 
		 * @param e
		 * 
		 */
		private function clickHandler(e:Event):void{
			//按钮组被锁定返回
			if(lock) return;
			var switchFrom:Object = null;
			var switchTo:Object = null;
			var i:int;
			var len:int = this.items.length;
			for(i=0;i<len;i++){
				if(items[i].target.selected){
					switchFrom = items[i].target;
				}
				if(e.currentTarget == items[i].target){
					items[i].target.selected = true;
					switchTo = items[i].target;
				}else{
					items[i].target.selected = false;
				}
			}
			this.onSwitch(switchFrom,switchTo);
		}
		
		public function set lock(value:Boolean):void{
			this._lock = value;
		}
		
		public function get lock():Boolean{
			return this._lock;
		}
		
		/**
		 *添加一个按钮组元素
		 * 用 obj对象包装
		 * obj.target -- 按钮对象
		 * obj.oldOnSelect -- 按钮对象原来的侦听
		 * obj.target.onSelect -- 按钮对象新的侦听
		 * 
		 * @param item  item 要有 onSelect 和 selected 属性
		 * onSelect 属性 会被赋予 clickHandlert
		 * 
		 */
		public function addItem(item:Object):void{
			if(item==null){
				return;
			}
			var obj:Object = this.findAddeditem(item); 
			if(obj!=null) {
				if(item.onSelect!=clickHandler){
					obj.oldOnSelect = item.onSelect;
				}
				item.onSelect = clickHandler;
				(item as DisplayObject).addEventListener(Event.REMOVED_FROM_STAGE, onDestory);
				return;
			}
			item.selected = false;
			obj = new Object();
			obj.target = item;
			obj.oldOnSelect = item.onSelect;
			item.onSelect = clickHandler;
			if (item is DisplayObject)
			{
				(item as DisplayObject).addEventListener(Event.REMOVED_FROM_STAGE, onDestory);
			}
			this.items.push(obj);
		}
		
		public function addItems(itemList:Array):void{
			if(itemList==null){
				return;
			}
			var i:int;
			var l:int = itemList.length;
			for(i=0; i<l; i++){
				this.addItem(itemList[i]);
			}
			
		}
		
		/**
		 *移除舞台自动销毁 
		 * @param evt
		 * 
		 */
		private function onDestory(evt: Event): void
		{
			(evt.currentTarget as DisplayObject).removeEventListener(Event.REMOVED_FROM_STAGE, onDestory);
			removeItem(evt.currentTarget as ISelectable);
		}
		
		public function clear(removeSwitch:Boolean=false):void{
			if(removeSwitch){
				this.onSwitch = null;
			}
			var items:Array = this.itemsArray;
			var i:int,l:int;
			for(i=0,l=items.length;i<l;i++){
				this.removeItem(items[i]);
			}
		}		
		
		public function removeItem(item:Object):void{
			var obj:Object = this.findAddeditem(item);
			if(obj == null) return;
			item.onSelect = null;
			obj.target.onSelect = obj.oldOnSelect;
			delete(obj.oldOnSelect);
			delete(obj.target);
			ArrayTools.del(items,obj);
		}
		
		public function getIndexOf(item:ISelectable):int{
			for(var i:uint = 0,l:uint = this.items.length;i<l;i++){
				if(items[i].target == item){
					return i;
				}
			}
			return -1;			
		}
		
		public function get selectedItem():ISelectable{
			for(var i:uint = 0,l:uint = this.items.length;i<l;i++){
				if(items[i].target.selected){
					return items[i].target;
				}
			}
			return null;
		}
		
		public function get selectedItemIndex():int{
			for(var i:uint = 0,l:uint = this.items.length;i<l;i++){
				if(items[i].target.selected){
					return i;
				}
			}
			return -1;			
		}
		
		public function setSelectedItem(value:Boolean,item:Object):void{
			var obj:Object = this.findAddeditem(item); 
			if(obj==null) return;
			if(value){
				obj.target.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
			
		}
		
		public function setSelectedItemByIndex(value:Boolean,index:int):void{
			if(index>=this.items.length||index<0) return;
			if(value){
				this.items[index].target.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}

	}
}